<?php
namespace app\system\controller;

class Shop extends Common
{
    protected $pageSize = 20;

    /**
     * 列表
     * @param string $keywords
     * @return mixed
     */
    public function index($keywords='')
    {
        $keywords = trim($keywords);
        $where = [];
        if (!empty($keywords)) {
            $where['title'] = ['like', "%$keywords%"];
        }
        $pageconfig['path'] = url("?keywords=$keywords");

        $model = new \app\common\model\Shop();
        $list = $model->getList($where, $this->pageSize, $pageconfig);

        $assign = [
            'list' => $list,
        ];
        return $this->fetch('', $assign);
    }

    /**
     * 删除
     * @param $id
     * @return \think\response\Json
     */
    public function del($id)
    {
        if (isAjaxPost()) {
            $id = intval($id);
            if ($id <= 0) {
                return $this->jsonError('删除失败');
            }

            $model = new \app\common\model\Shop();
            if ($model->where(['id'=>$id])->delete() !== false) {
                return $this->jsonSuccess('删除成功');
            } else {
                return $this->jsonError('删除失败');
            }
        }
    }

    /**
     * 添加/编辑
     * @param int $id
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function save($id=0) {
        $model = new \app\common\model\Shop();
        if (isAjaxPost()) {
            $data = input('post.');
            $validate = new \app\system\validate\Shop();
            if (!$validate->check($data)) {
                return $this->jsonError($validate->getError());
            }

            unset($data['id']);
            if ($id > 0) {
                if ($model->update($data, ['id'=>$id]) !== false) {
                    return $this->jsonSuccess('修改成功');
                } else {
                    return $this->jsonError('修改失败');
                }
            } else {
                if ($model->insert($data)) {
                    return $this->jsonSuccess('添加成功');
                } else {
                    return $this->jsonError('添加失败');
                }
            }
        }

        if ($id > 0) {
            $this->assign('data', $model->where(['id'=>$id])->find());
        }

        return $this->fetch();
    }

    /**
     * 批量删除
     * */
    public function delAll()
    {
        if (isAjaxPost()) {
            $post = input('post.');

            if (empty($post['id']) || !is_array($post['id']) || count($post['id']) <= 0) {
                return $this->jsonError('请勾选需要删除的用户！');
            }
            $ids = $post['id'];

            $model = new \app\common\model\Shop();
            if ($model->where(['id'=>['in', $ids]])->delete() !== false) {
                return $this->jsonSuccess('删除成功');
            } else {
                return $this->jsonError('删除失败');
            }
        }
    }
}